<html>
<head>
    <title>cometd-yui3 test cases - subscribe, unsubscribe etc.</title>
    <!--<script type="text/javascript" src="http://yui.yahooapis.com/3.0.0pr2/build/yui/yui-min.js"></script>-->
    <!--<script type="text/javascript" src="../cometd-yui3/cometd-yui3.js"></script>-->
    <script src="../cometd-yui3/cometd-yui3-loader.js" type="text/javascript"></script>
</head>
<body class="yui-skin-sam">
<h1>
    <script>document.write(document.title)</script>
</h1>
<script type="text/javascript">
    comet.YUI({base:'../yui3/',cometBase:'../cometd-yui3/'}).use('yuitest', 'console', 'comet', 'chain', 'queue', 'io-base', function(Y) {

        Y.namespace('comet.tests')
        Y.comet.tests.Tests = new Y.Test.Case({
            name:'Subscribe Tests',
            setUp:function() {
                this.data = {
                    key:'value'
                }
            },
            tearDown:function() {
                delete this.data;
            },

            testQueue:function() { // default as adviceEnabled
                var state = 0;
                var chain = new Y.Chain();
                chain.add(Y.bind(function() {
                    Y.Assert.areEqual(0, state)
                    Y.log('task1 begin', 'warn', 'test')
                    state = 1
                    var req = Y.io('/', {
                        on:{
                            complete:function() {
                                Y.Assert.areEqual(1, state)
                                state = 2
                                Y.log('task1 completed', 'warn', 'test')
                            }
                        }
                    })
                }, this))
                chain.add(Y.bind(function() {
                    Y.Assert.areEqual(2, state)
                    state = 3
                    Y.log('task2 begin', 'warn', 'test')
                    var req = Y.io('/', {
                        on:{
                            complete:function() {
                                Y.Assert.areEqual(3, state)
                                state = 4
                                Y.log('task2 completed', 'warn', 'test')
                            }
                        }
                    })
                }, this))
                chain.run()
                //this.wait(function() { Y.Assert.fail('fail to trigger the \'task1\' event with 1000ms')}, 1000);
            },

            XLastFunction:function() {
            }
        })
        //TODO add test cases about handleAdvice

        Y.comet.tests.DevTests = new Y.Test.Case({
            name: 'Development Tests',
            setUp:function() {
                this.data = {
                }
            },
            tearDown:function() {
                delete this.data;
            },

            XtestSubscribePublish:function() {
                var channelName = '/testSubscribeMessage'
                var comet = new Y.Comet({uri:'.cometd'});

                comet.on('connect', function() {
                    this.resume(function() {
                        Y.Assert.areEqual('connected', comet.getClientState())
                        comet.subscribe({channel:channelName})
                        this.wait(function() { Y.Assert.fail('fail to trigger the \'subscribe\' event with 1000ms')}, 1000);
                    })
                }, this)
                comet.on('subscribe', function(r) {
                    this.resume(function() {
                        //Y.Assert.areEqual('connected', comet.getClientState())
                        Y.Assert.isTrue(r.successful)
                        Y.Assert.areEqual(channelName, r.subscription)
                        comet.publish({channel:channelName, data:{key0:'value0'}})
                        this.wait(function() { Y.Assert.fail('fail to trigger the \'publish\' event with 1000ms')}, 1000);
                    })
                }, this)
                comet.on('publish', function(r) {
                    this.resume(function() {
                        Y.Assert.isTrue(r.successful)
                        Y.log('published a message')
                        //TODO add test case to assert the receiving message
                    })
                }, this)

                comet.connect();
                this.wait(function() { Y.Assert.fail('fail to trigger the \'handshake\' event with 1000ms')}, 1000);
            },
            XLastFunction:function() {
            }
        })

        //TODO: https://sourceforge.net/tracker2/?func=detail&atid=836476&aid=2431381&group_id=165715
        var console = new Y.Console()
        console.get('contentBox').setStyle('width', (Y.get('body').get('docWidth') * 3 / 4) + 'px')
        console.render()
        console.get('contentBox').query('div .yui-console-bd').setStyle('height', (Y.get('body').get('docHeight') * 4 / 5) + 'px')
        console.on('entry', function(e) {
            if (e.message.source !== 'TestRunner') {
                e.preventDefault()
            }
        })

        Y.Test.Runner.add(Y.comet.tests.DevTests);
        if (!Y.Comet.any(Y.comet.tests.DevTests, function(v, k) {
            return /^test/.test(k)
        })) {
            // if there are any development tests, normal test won't run
            Y.Test.Runner.add(Y.comet.tests.Tests);
        }
        Y.Test.Runner.run();
    })
</script>


</body>
</html>